﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Xml;
namespace BUS.Mail
{
    public class MailSender
    {
        private String m_SenderAddess ;

        private String m_SmtpServer ;

        private String m_UserName ;
        
        private String m_Password ;
        public MailSender()
        {
            ReadConfig();
        }
        public void SendMail(MailMessage mailMessage)
        {
            ReadConfig();
            mailMessage.From = new MailAddress(SenderAddess);
            SmtpClient client = new SmtpClient(SmtpServer);
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential(UserName, Password);
            client.Send(mailMessage);
        }

        public static String ConfigFilePath = System.IO.Path.GetDirectoryName(
                    System.Reflection.Assembly.GetAssembly(typeof(MailSender)).CodeBase)
                   + "/config/configmail.xml";
        /// <summary>
        /// Đọc file config/config.xml và trả ra đối tượng ConfigDTO tương ứng.
        /// </summary>
        /// <returns></returns>
        public void ReadConfig()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(ConfigFilePath);
            XmlElement eRoot = doc.DocumentElement;
            m_SenderAddess = eRoot.ChildNodes[0].InnerText;
            m_SmtpServer = eRoot.ChildNodes[1].InnerText;
            m_UserName = eRoot.ChildNodes[2].InnerText;
            m_Password = eRoot.ChildNodes[3].InnerText;
        }

        /// <summary>
        /// Lưu thộng tin config vào file config/config.xml
        /// </summary>
        /// <param name="config">Thông tin cấu hình cầu lưu.</param>
        /// <returns>true nếu thành công,  false nếu không thất bại(không tìm được file cấu hình)</returns>
        public bool SaveConfig()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                /*doc.Load(ConfigFilePath);
                XmlElement eRoot = doc.DocumentElement;
                eRoot.ChildNodes[0].InnerXml = m_SenderAddess;
                eRoot.ChildNodes[1].InnerXml = m_SmtpServer;
                eRoot.ChildNodes[2].InnerXml = m_UserName;
                eRoot.ChildNodes[3].InnerXml = m_Password;*/
                string xml="<?xml version='1.0' encoding='utf-8' ?>";
                xml+="<MailConfig><SenderAddress>"+m_SenderAddess+"</SenderAddress>";
                xml+="<SmtpServer>"+m_SmtpServer+"</SmtpServer>";
                xml+="<UserName>"+m_UserName+"</UserName>";
                xml += "<Password>" + m_Password + "</Password></MailConfig>";
                doc.LoadXml(xml);
                String path = ConfigFilePath.Substring(6);
                doc.Save(path);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                
            }
            return false;
        }


        

        /// <summary>
        /// Set hay get địa chỉ email người nhận.
        /// </summary>
        public String SenderAddess
        {
            get { return m_SenderAddess; }
            set { m_SenderAddess = value; }
        }

        /// <summary>
        /// Set hay get server smtp.
        /// </summary>
        public String SmtpServer
        {
            get { return m_SmtpServer; }
            set { m_SmtpServer = value; }
        }

        /// <summary>
        /// Set hay get username sử dụng smtp.
        /// </summary>
        public String UserName
        {
            get { return m_UserName; }
            set { m_UserName = value; }
        }

        /// <summary>
        /// Set hay get password.
        /// </summary>
        public String Password
        {
            get { return m_Password; }
            set { m_Password = value; }
        }
    }
}
